// releasePage/pages/operatorInfo/operatorInfo.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        review: '', //审核编辑标记位
        type: '00', //00 从测试点页面
        ipxBottom: 0,
        ftspelocationid: '',
        netId: '', //运营商网络类型id 用于再次编辑
        operId: '', //运营商id  用于查询
        operatorType: '',
        networktype: '',
        operatorValue: [
            [
                '5G:独立组网',
                '5G:ENDC',
                '5G:Band',
                '5G:PCI',
                '5G:RSRP',
                '5G:SINR',
                '5G:EARFCN',
                '4G:Band',
                '4G:PCI',
                '4G:RSRP',
                '4G:SINR',
                '4G:BW',
                '4G:Freq',
                '4G:EARFCN',
            ], //5g
            ['RSRP', 'SINR', 'Band', 'BW', 'CID', 'TAC', 'Freq', 'EARFCN'], //4G
            ['RSRP', 'SINR', 'PSC', 'BW', 'CI', 'LAC', 'RxLEV', 'UARFCN'], //3G-联通/移动
            ['EVDO DBM', 'EVDO ECIO', 'SID', 'NID', '1x DBM', '1x ECIO'], //3G-电信
            [
                'RSRP',
                'SINR',
                'Band',
                'BSIC',
                'CI',
                'LAC',
                'RxLEV',
                'Freq',
                'ARFCN',
            ], //2G-联通/移动
            ['SID', 'NID', '1x DBM', '1x ECIO'], //2G-电信
            [''], //无卡
        ],
        operatorindex: Number,
        operatorId: '',
        operator: {},
        editOperatorValue: [],
        submitBtn: true, //提交防抖
        deleteBtn: true, //删除防抖
    },
    operatorTap() {
        wx.navigateTo({
            url: `./operatorInter/operatorInter?operatorId=${
                this.data.operatorId || this.data.operId
            }&typeValue=${this.data.networktype}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        this.setData({
            ftspelocationid: options.id, //ft测试点id
            netId: options.netId, //运营商id
            operId: options.operid, //基站参数id
            type: options.type, //01 从编辑页面跳转
            review: options.review || '',
        });
        if (options.netId) {
            this.getOperatorInfo(options.operid);
        }
        // if (options.type) {
        //     this.getInfo();
        // }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    operatorContent(index) {
        let _that = this;
        for (let i = 0; i < _that.data.operatorValue[index].length; i++) {
            let operatorName = 'operator.' + _that.data.operatorValue[index][i];
            _that.setData({
                [operatorName]: '',
            });
        }
    },
    //发布页面再次编辑
    // getInfo() {
    //     let _that = this;
    //     let pages = getCurrentPages(); //当前页面栈
    //     let prevPage = pages[pages.length - 2]; //上一页面
    //     if (!prevPage.data.ftnetworktype) return;
    //     let index;
    //     if (prevPage.data.ftnetworktype == '无信号') {
    //         index = 6; //无卡
    //     } else if (prevPage.data.ftnetworktype == '5G') {
    //         index = 0; //5G
    //     } else if (prevPage.data.ftnetworktype == '4G') {
    //         index = 1; //4G
    //     } else if (prevPage.data.ftnetworktype == '3G') {
    //         if (res.data.ftOperatorInfo.networksupport.includes('08')) {
    //             index = 3; //3G 联通-移动
    //         } else {
    //             index = 2; //3G 电信
    //         }
    //     } else if (prevPage.data.ftnetworktype == '2G') {
    //         if (res.data.ftOperatorInfo.networksupport.includes('11')) {
    //             index = 5; //2G 联通-移动
    //         } else {
    //             index = 4; //2G 电信
    //         }
    //     }
    //     let values = JSON.parse(prevPage.data.ftoperatorinfo);
    //     _that.setData({
    //         operatorindex: index,
    //         operatorType:
    //             prevPage.data.ftOperator + '/' + prevPage.data.ftnetworktype,
    //         editOperatorValue: Object.values(values),
    //     });
    //     _that.operatorContent(index);
    //     console.log(values);
    //     for (let i = 0; i < Object.values(values).length; i++) {
    //         if (Object.values(values)[i]) {
    //             let operatorName = 'operator.' + Object.keys(values)[i];
    //             _that.setData({
    //                 [operatorName]: Object.values(values)[i],
    //             });
    //             console.log(operatorName);
    //         }
    //     }
    // },
    //再次编辑获取网络内容
    getOperatorInfo(e) {
        let _that = this;
        console.log(e);
        wx.request({
            url:
                app.globalData.rootPath +
                `cjMap/getFtOperatorInfoDetail?authId=${app.globalData.authId}&id=${e}&ftspeciallocationid=${_that.data.ftspelocationid}`,
            method: 'GET',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                console.log(res.data);
                if (res.data.ftOperatorInfo.networktype) {
                    let index;
                    if (res.data.ftOperatorInfo.networktype == '无信号') {
                        index = 6; //无卡
                    } else if (res.data.ftOperatorInfo.networktype == '5G') {
                        index = 0; //5G
                    } else if (res.data.ftOperatorInfo.networktype == '4G') {
                        index = 1; //4G
                    } else if (res.data.ftOperatorInfo.networktype == '3G') {
                        if (
                            res.data.ftOperatorInfo.networksupport.includes(
                                '08'
                            )
                        ) {
                            index = 3; //3G 联通-移动
                        } else {
                            index = 2; //3G 电信
                        }
                    } else if (res.data.ftOperatorInfo.networktype == '2G') {
                        if (
                            res.data.ftOperatorInfo.networksupport.includes(
                                '11'
                            )
                        ) {
                            index = 5; //2G 联通-移动
                        } else {
                            index = 4; //2G 电信
                        }
                    }
                    let values = JSON.parse(
                        res.data.ftOperatorInfo.operatorinfo
                    );
                    _that.setData({
                        operatorindex: index,
                        operatorType: res.data.ftOperatorInfo.operator,
                        editOperatorValue: Object.values(values),
                        networktype: res.data.ftOperatorInfo.networktype,
                    });
                    _that.operatorContent(index);
                    console.log(values);
                    for (let i = 0; i < Object.values(values).length; i++) {
                        if (Object.values(values)[i]) {
                            let operatorName =
                                'operator.' + Object.keys(values)[i];
                            _that.setData({
                                [operatorName]: Object.values(values)[i],
                            });
                            console.log(operatorName);
                        }
                    }
                } else {
                    _that.setData({
                        operatorType: res.data.ftOperatorInfo.operator,
                    });
                }
                console.log(_that.data.editOperatorValue);
            },
        });
    },
    operatorInput(e) {
        let operatorName = 'operator.' + e.currentTarget.dataset.name;
        this.setData({
            [operatorName]: e.detail.value,
        });
        app.numLimitTip(e.detail.value.length, 10);
    },
    // 提交
    submit(e) {
        let _that = this;
        if (app.visitorPublish()) return; //游客发布限制
        if (!_that.data.submitBtn) return;
        if (_that.data.operatorType) {
            if (!_that.data.netId) {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/createFtOperatorInfo?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        delflg: '02', //删除标志 00未删除 01已删除 02待审核10草稿
                        ftspelocationid: _that.data.ftspelocationid,
                        networktype: _that.data.networktype,
                        operator: _that.data.operatorType,
                        operatorinfo: JSON.stringify(_that.data.operator),
                        cityoperatorid: _that.data.operatorId,
                    },
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 0) {
                            app.globalData.indexExpRefresh = true;
                            _that.setData({
                                submitBtn: false,
                            });
                            wx.showToast({
                                title: '提交成功',
                                icon: 'success',
                                duration: 2000,
                            });
                            let pages = getCurrentPages(); //当前页面栈
                            let prevPage = pages[pages.length - 2]; //上一页面
                            let prevPage2 = pages[pages.length - 3]; //上一页面
                            prevPage.refresh();
                            if (
                                prevPage2 &&
                                typeof prevPage2.refresh == 'function'
                            ) {
                                try {
                                    let arr = ['ftPoint'];
                                    prevPage2.refresh(arr);
                                } catch (err) {
                                    prevPage2.refresh();
                                }
                            }
                            setTimeout(function () {
                                wx.navigateBack({
                                    delta: 1, // 返回上一级页面。
                                });
                            }, 2000);
                        } else {
                            wx.showToast({
                                title: '提交失败，请重新录入或联系后台管理',
                                icon: 'none',
                                duration: 2000,
                            });
                            setTimeout(function () {
                                wx.navigateBack({
                                    delta: 1, // 返回上一级页面。
                                });
                            }, 2000);
                        }
                    },
                });
            } else {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/editFtOperatorInfo?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        id: _that.data.netId,
                        delflg: '02', //删除标志 00未删除 01已删除 02待审核10草稿
                        ftspelocationid: _that.data.ftspelocationid,
                        networktype: _that.data.networktype,
                        operator: _that.data.operatorType,
                        operatorinfo: JSON.stringify(_that.data.operator),
                        cityoperatorid:
                            _that.data.operatorId || _that.data.operId,
                    },
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 0) {
                            app.globalData.indexExpRefresh = true;
                            if (!_that.data.review) {
                                _that.setData({
                                    submitBtn: false,
                                });
                                wx.showToast({
                                    title: '提交成功',
                                    icon: 'success',
                                    duration: 2000,
                                });
                                let pages = getCurrentPages(); //当前页面栈
                                let prevPage = pages[pages.length - 2]; //上一页面
                                let prevPage2 = pages[pages.length - 3]; //上一页面
                                prevPage.refresh();
                                if (
                                    prevPage2 &&
                                    typeof prevPage2.refresh == 'function'
                                ) {
                                    try {
                                        let arr = ['ftPoint'];
                                        prevPage2.refresh(arr);
                                    } catch (err) {
                                        prevPage2.refresh();
                                    }
                                }
                                setTimeout(function () {
                                    wx.navigateBack({
                                        delta: 1, // 返回上一级页面。
                                    });
                                }, 2000);
                            } else {
                                console.log(res);
                                app.doVerify(res.data.data.id, '16');
                            }
                        } else {
                            wx.showToast({
                                title: '提交失败，请重新录入或联系后台管理',
                                icon: 'none',
                                duration: 2000,
                            });
                            setTimeout(function () {
                                wx.navigateBack({
                                    delta: 1, // 返回上一级页面。
                                });
                            }, 2000);
                        }
                    },
                });
            }
        }
    },

    // 删除
    deleteBtn(e) {
        let _that = this;
        if (!_that.data.deleteBtn) return;
        wx.showModal({
            title: '提示：',
            content: '确认要删除？',
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                        url:
                            app.globalData.rootPath +
                            `cjMap/deleteFtOperatorInfo`,
                        method: 'GET',
                        header: {
                            'content-type': 'application/x-www-form-urlencoded',
                        },
                        data: {
                            authId: app.globalData.authId,
                            id: _that.data.netId,
                        },
                        success: function (res) {
                            _that.setData({
                                deleteBtn: false,
                            });
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                duration: 2000,
                            });
                            let pages = getCurrentPages(); //当前页面栈
                            let prevPage2 = pages[pages.length - 3]; //上一页面
                            prevPage.refresh();
                            if (
                                prevPage2 &&
                                typeof prevPage2.refresh == 'function'
                            ) {
                                try {
                                    let arr = ['ftPoint'];
                                    prevPage2.refresh(arr);
                                } catch (err) {
                                    prevPage2.refresh();
                                }
                            }
                            setTimeout(function () {
                                wx.navigateBack({
                                    delta: 1, // 返回上一级页面。
                                });
                            }, 2000);
                        },
                    });
                } else {
                    console.log('点击取消回调');
                }
            },
        });
    },
});
